home *** CD-ROM | disk | FTP | other *** search
/ Gamers Delight 2 / Gamers Delight 2.iso / Aminet / game / role / ZIP_1_0.lha / src / property.c < prev    next >
C/C++ Source or Header  |  1992-10-13  |  10KB  |  464 lines

  1. /*
  2.  * property.c
  3.  *
  4.  * Property manipulation routines
  5.  *
  6.  * Mark Howell 28-Jul-1992 V1.0
  7.  *
  8.  */
  9.  
  10. #include "ztypes.h"
  11.  
  12. /*
  13.  * get_property_addr
  14.  *
  15.  * Calculate the address of the start of the property list associated with an
  16.  * object.
  17.  *
  18.  */
  19.  
  20. #ifdef __STDC__
  21. static zword_t get_property_addr (zword_t obj)
  22. #else
  23. static zword_t get_property_addr (obj)
  24. zword_t obj;
  25. #endif
  26. {
  27.     zword_t offset, prop;
  28.  
  29.     /* Calculate the address of the property pointer in the object */
  30.  
  31.     offset = get_object_address (obj);
  32.     offset += (h_type == V3) ? O3_PROPERTY_OFFSET : O4_PROPERTY_OFFSET;
  33.  
  34.     /* Read the property pointer */
  35.  
  36.     prop = get_word (offset);
  37.  
  38.     /* Skip past object description which is an ASCIC of encoded words */
  39.  
  40.     prop += (get_byte (prop) * 2) + 1;
  41.  
  42.     return (prop);
  43.  
  44. }/* get_property_adr */
  45.  
  46. /*
  47.  * get_next_property
  48.  *
  49.  * Calculate the address of the next property in a property list.
  50.  *
  51.  */
  52.  
  53. #ifdef __STDC__
  54. static zword_t get_next_property (zword_t propp)
  55. #else
  56. static zword_t get_next_property (propp)
  57. zword_t propp;
  58. #endif
  59. {
  60.     zbyte_t value;
  61.  
  62.     /* Load the current property id */
  63.  
  64.     value = get_byte (propp++);
  65.  
  66.     /* Calculate the length of this property */
  67.  
  68.     if (h_type == V3)
  69.         value = (zbyte_t) ((value & property_size_mask) >> 5);
  70.     else if (value & 0x80)
  71.         value = get_byte (propp) & (zbyte_t) property_size_mask;
  72.     else if (value & 0x40)
  73.         value = 1;
  74.     else
  75.         value = 0;
  76.  
  77.     /* Address property length + 1 to current property pointer */
  78.  
  79.     return (propp + value + 1);
  80.  
  81. }/* get_next_property */
  82.  
  83. /*
  84.  * load_property
  85.  *
  86.  * Load a property from a property list. Properties are held in list sorted by
  87.  * property id, with highest ids first. There is also a concept of a default
  88.  * property for loading only. The default properties are held in a table pointed
  89.  * to be the object pointer, and occupy the space before the first object.
  90.  *
  91.  */
  92.  
  93. #ifdef __STDC__
  94. void load_property (zword_t obj, zword_t prop)
  95. #else
  96. void load_property (obj, prop)
  97. zword_t obj;
  98. zword_t prop;
  99. #endif
  100. {
  101.     zword_t propp;
  102.  
  103.     /* Load address of first property */
  104.  
  105.     propp = get_property_addr (obj);
  106.  
  107.     /* Scan down the property list while the target property id is less than the
  108.        property id in the list */
  109.  
  110.     while ((zbyte_t) (get_byte (propp) & property_mask) > (zbyte_t) prop)
  111.         propp = get_next_property (propp);
  112.  
  113.     /* If the property ids match then load the first property */
  114.  
  115.     if ((zbyte_t) (get_byte (propp) & property_mask) == (zbyte_t) prop) {
  116.  
  117.         /* Only load first property if it is a byte sized property */
  118.  
  119.         if ((get_byte (propp++) & property_size_mask) == 0) {
  120.             store_operand (get_byte (propp));
  121.             return;
  122.         }
  123.     } else
  124.  
  125.         /* Calculate the address of the default property */
  126.  
  127.         propp = h_objects_offset + ((prop - 1) * 2);
  128.  
  129.     /* Load the first property word */
  130.  
  131.     store_operand (get_word (propp));
  132.  
  133. }/* load_property */
  134.  
  135. /*
  136.  * store_property
  137.  *
  138.  * Store a property value in a property list. The property must exist in the
  139.  * property list to be replaced.
  140.  *
  141.  */
  142.  
  143. #ifdef __STDC__
  144. void store_property (zword_t obj, zword_t prop, zword_t value)
  145. #else
  146. void store_property (obj, prop, value)
  147. zword_t obj;
  148. zword_t prop;
  149. zword_t value;
  150. #endif
  151. {
  152.     zword_t propp;
  153.  
  154.     /* Load address of first property */
  155.  
  156.     propp = get_property_addr (obj);
  157.  
  158.     /* Scan down the property list while the target property id is less than the
  159.        property id in the list */
  160.  
  161.     while ((zbyte_t) (get_byte (propp) & property_mask) > (zbyte_t) prop)
  162.         propp = get_next_property (propp);
  163.  
  164.     /* If the property id was found then store a new value, otherwise complain */
  165.  
  166.     if ((zbyte_t) (get_byte (propp) & property_mask) == (zbyte_t) prop) {
  167.  
  168.         /* Determine if this is a byte or word sized property */
  169.  
  170.         if ((get_byte (propp++) & property_size_mask) == 0)
  171.             set_byte (propp, value);
  172.         else
  173.             set_word (propp, value);
  174.     } else
  175.         fatal ("No such property");
  176.  
  177. }/* store_property */
  178.  
  179. /*
  180.  * load_next_property
  181.  *
  182.  * Load the property after the current property. If the current property is zero
  183.  * then load the first property.
  184.  *
  185.  */
  186.  
  187. #ifdef __STDC__
  188. void load_next_property (zword_t obj, zword_t prop)
  189. #else
  190. void load_next_property (obj, prop)
  191. zword_t obj;
  192. zword_t prop;
  193. #endif
  194. {
  195.     zword_t propp;
  196.  
  197.     /* Load address of first property */
  198.  
  199.     propp = get_property_addr (obj);
  200.  
  201.     /* If the property id is non zero then find the next property */
  202.  
  203.     if (prop) {
  204.  
  205.         /* Scan down the property list while the target property id is less than the
  206.            property id in the list */
  207.  
  208.         while ((zbyte_t) (get_byte (propp) & property_mask) > (zbyte_t) prop)
  209.             propp = get_next_property (propp);
  210.  
  211.         /* If the property id was found then get the next property, otherwise complain */
  212.  
  213.         if ((zbyte_t) (get_byte (propp) & property_mask) == (zbyte_t) prop)
  214.             propp = get_next_property (propp);
  215.         else
  216.             fatal ("No such property");
  217.     }
  218.  
  219.     /* Return the next property id */
  220.  
  221.     store_operand (get_byte (propp) & property_mask);
  222.  
  223. }/* load_next_property */
  224.  
  225. /*
  226.  * load_property_address
  227.  *
  228.  * Load the address address of the data associated with a property.
  229.  *
  230.  */
  231.  
  232. #ifdef __STDC__
  233. void load_property_address (zword_t obj, zword_t prop)
  234. #else
  235. void load_property_address (obj, prop)
  236. zword_t obj;
  237. zword_t prop;
  238. #endif
  239. {
  240.     zword_t propp;
  241.  
  242.     /* Load address of first property */
  243.  
  244.     propp = get_property_addr (obj);
  245.  
  246.     /* Scan down the property list while the target property id is less than the
  247.        property id in the list */
  248.  
  249.     while ((zbyte_t) (get_byte (propp) & property_mask) > (zbyte_t) prop)
  250.         propp = get_next_property (propp);
  251.  
  252.     /* If the property id was found then calculate the property address, otherwise return zero */
  253.  
  254.     if ((zbyte_t) (get_byte (propp) & property_mask) == (zbyte_t) prop) {
  255.  
  256.         /* Skip past property id, can be a byte or a word */
  257.  
  258.         if (h_type != V3 && (get_byte (propp) & 0x80))
  259.             propp++;
  260.         propp++;
  261.         store_operand (propp);
  262.     } else
  263.  
  264.         /* No property found, just return 0 */
  265.  
  266.         store_operand (0);
  267.  
  268. }/* load_property_address */
  269.  
  270. /*
  271.  * load_property_length
  272.  *
  273.  * Load the length of a property.
  274.  *
  275.  */
  276.  
  277. #ifdef __STDC__
  278. void load_property_length (zword_t propp)
  279. #else
  280. void load_property_length (propp)
  281. zword_t propp;
  282. #endif
  283. {
  284.  
  285.     /* Back up the property pointer to the property id */
  286.  
  287.     propp--;
  288.  
  289.     if (h_type == V3)
  290.  
  291.         /* Property length is in high bits of property id */
  292.  
  293.         store_operand (((get_byte (propp) & property_size_mask ) >> 5) + 1);
  294.     else if (get_byte (propp) & 0x80)
  295.  
  296.         /* Property length is in property id */
  297.  
  298.         store_operand (get_byte (propp) & property_size_mask);
  299.     else
  300.  
  301.         /* Word sized property if bit 6 set, else byte sized property */
  302.  
  303.         store_operand ((get_byte (propp) & 0x40) ? 2 : 1);
  304.  
  305. }/* load_property_length */
  306.  
  307. /*
  308.  * scan_word
  309.  *
  310.  * Scan an array of words looking for a target word.
  311.  *
  312.  */
  313.  
  314. #ifdef __STDC__
  315. void scan_word (zword_t search, zword_t addr, zword_t count)
  316. #else
  317. void scan_word (search, addr, count)
  318. zword_t search;
  319. zword_t addr;
  320. zword_t count;
  321. #endif
  322. {
  323.     unsigned long address;
  324.     unsigned int i;
  325.  
  326.     address = addr;
  327.  
  328.     /* Scan down an array for count words looking for a match */
  329.  
  330.     for (i = 0; i < count; i++)
  331.  
  332.         /* If the word was found store its address and jump */
  333.  
  334.         if (read_data_word (&address) == search) {
  335.             store_operand ((zword_t) (address - 2));
  336.             conditional_jump (TRUE);
  337.             return;
  338.         }
  339.  
  340.     /* If the word was not found store zero and jump */
  341.  
  342.     store_operand (0);
  343.     conditional_jump (FALSE);
  344.  
  345. }/* scan_word */
  346.  
  347. /*
  348.  * load_word
  349.  *
  350.  * Load a word from an array of words
  351.  *
  352.  */
  353.  
  354. #ifdef __STDC__
  355. void load_word (zword_t addr, zword_t offset)
  356. #else
  357. void load_word (addr, offset)
  358. zword_t addr;
  359. zword_t offset;
  360. #endif
  361. {
  362.     unsigned long address;
  363.  
  364.     /* Calculate word array index address */
  365.  
  366.     address = addr + (offset * 2);
  367.  
  368.     /* Store the byte */
  369.  
  370.     store_operand (read_data_word (&address));
  371.  
  372. }/* load_word */
  373.  
  374. /*
  375.  * load_byte
  376.  *
  377.  * Load a byte from an array of bytes
  378.  *
  379.  */
  380.  
  381. #ifdef __STDC__
  382. void load_byte (zword_t addr, zword_t offset)
  383. #else
  384. void load_byte (addr, offset)
  385. zword_t addr;
  386. zword_t offset;
  387. #endif
  388. {
  389.     unsigned long address;
  390.  
  391.     /* Calculate byte array index address */
  392.  
  393.     address = addr + offset;
  394.  
  395.     /* Load the byte */
  396.  
  397.     store_operand (read_data_byte (&address));
  398.  
  399. }/* load_byte */
  400.  
  401. /*
  402.  * store_word
  403.  *
  404.  * Store a word in an array of words
  405.  *
  406.  */
  407.  
  408. #ifdef __STDC__
  409. void store_word (zword_t addr, zword_t offset, zword_t value)
  410. #else
  411. void store_word (addr, offset, value)
  412. zword_t addr;
  413. zword_t offset;
  414. zword_t value;
  415. #endif
  416. {
  417.  
  418.     /* Calculate word array index address */
  419.  
  420.     addr += offset * 2;
  421.  
  422.     /* Check we are not writing outside of the writeable data area */
  423.  
  424.     if (addr > data_size)
  425.         fatal ("Attempted write out of data area");
  426.  
  427.     /* Store the word */
  428.  
  429.     set_word (addr, value);
  430.  
  431. }/* store_word */
  432.  
  433. /*
  434.  * store_byte
  435.  *
  436.  * Store a byte in an array of bytes
  437.  *
  438.  */
  439.  
  440. #ifdef __STDC__
  441. void store_byte (zword_t addr, zword_t offset, zword_t value)
  442. #else
  443. void store_byte (addr, offset, value)
  444. zword_t addr;
  445. zword_t offset;
  446. zword_t value;
  447. #endif
  448. {
  449.  
  450.     /* Calculate byte array index address */
  451.  
  452.     addr += offset;
  453.  
  454.     /* Check we are not writing outside of the writeable data area */
  455.  
  456.     if (addr > data_size)
  457.         fatal ("Attempted write out of data area");
  458.  
  459.     /* Store the byte */
  460.  
  461.     set_byte (addr, value);
  462.  
  463. }/* store_byte */
  464.